package com.cloud.bssp.thread.sync;

import java.util.concurrent.CountDownLatch;

/**
 * @description： 线程共享模型问题
 * @author：weirx
 * @date：2021/11/25 9:48
 * @version：3.0
 */
public class ThreadSharedModelProblems {

    /**
     * 两个长度的门闩
     */
    static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {
        Room room = new Room();

        Thread t1 = new Thread(() -> {
            for (int j = 0; j < 100000; j++) {
                room.add();
            }
            // 减少门闩数
            countDownLatch.countDown();
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            for (int j = 0; j < 100000; j++) {
                room.add();
            }
            // 减少门闩数
            countDownLatch.countDown();
        });
        t2.start();
        //阻塞等待门闩数降为0
        countDownLatch.await();

        System.out.println("i = " + room.getI());
    }
}
